<?php
    if ( isset($_GET['status']))
        $error = "Protocolo não encontrado!";
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Sistema de Protocolos RCS</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />  
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>  
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>  

    <link type="text/css" rel="stylesheet" href="css/reset.css" />
    <link type="text/css" rel="stylesheet" href="css/default.css" />
    <link type="text/css" rel="stylesheet" href="css/index.css" />
    
    <style>
            body { font-size: 64.5%; }
            label, input { display:block; }
            input.text { margin-bottom:12px; width:95%; padding: .4em; }
            fieldset { padding:0; border:0; margin-top:25px; }
            h1 { font-size: 1.2em; margin: .6em 0; }
            div#users-contain { width: 350px; margin: 20px 0; }
            div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
            div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
            .ui-dialog .ui-state-error { padding: .3em; }
            .validateTips { border: 1px solid transparent; padding: 0.3em; }
    </style>
    
    <script>
        
        function verificaProtocolo(){
            var numeroProtocolo = document.getElementById("numeroProtocolo").value;
            var isProtocolo = new RegExp(/^[A-Z]{3}\-\d{10}\/\d{4}$/);
            
            // recupera o elemento div do html
            var divError = document.getElementById("divError");
            divError.style.visibility="visible"; // torna visível
            
            if (numeroProtocolo !== "")
                if ( isProtocolo.test(numeroProtocolo) === true ){
                    divError.style.border="1px dotted #0000FF";
                    divError.style.backgroundColor="blue";
                    divError.style.color="#FFFFFF";
                    divError.innerHTML="Formato Verificado!";
                    return true;
                }else{
                    divError.style.border="1px dotted #FF0000";
                    divError.style.backgroundColor="red";
                    divError.style.color="#ffffff";
                    divError.innerHTML="Formato inválido!";
                    return false;
                }
                
            divError.style.border="1px dotted #FF0000";
            divError.style.backgroundColor="orange";
            divError.style.color="#ffffff";
            divError.innerHTML="Por favor entre com o número do protocolo.";
            return false;
        }

        $(function() {
            $( "#dialog-form" ).dialog({
                autoOpen: true,
                height: 230,
                width: 350,
                modal: true,
                draggable: false,
                buttons: {
                        "Consultar": function() 
                        { 
                            if ( verificaProtocolo() == true ){
                                //window.location.href="php/consultar-protocolo.php?PN=" + numeroProtocolo;
                                document.getElementById("formConsultarProtocolo").submit();
                                $( this ).dialog( "close" );
                            }
                        }
                },

                close: function() {
                    allFields.val( "" ).removeClass( "ui-state-error" );
                }
            });
        });

  </script>
</head>
<body>
    <div id="divTodoConteudo">
        <div id="dialog-form" title="Consultar Protocolo">
        <p id="divError" class="validateTips"> Formato do Protocolo: XXXX/XXXXXXXXX </p>  
        
<?php
        // se o protocolo não for encontrado
        if ( isset($error))
        echo'<script>
                var divError = document.getElementById("divError");
                divError.style.visibility="visible";
                divError.style.border="1px dotted #FF0000";
                divError.style.backgroundColor="red";
                divError.style.color="#ffffff";
                divError.innerHTML="' . $error . '";
            </script>';
?>
            
            
        <form id="formConsultarProtocolo" action="protocolo/protocolo.php" method="POST" onsubmit="return verificaProtocolo()" >
            <fieldset>
                <label for="numeroProtocolo"> Número do Protocolo </label>
                <input type="text" onblur="return verificaProtocolo();" name="numeroProtocolo" id="numeroProtocolo" class="text ui-widget-content ui-corner-all" title="XXXX/XXXXXXXXXX" required />
            </fieldset>
            </form>
        </div>
    </div>
</body>
</html>